package com.cloud.wms.ui.main.entity;

import android.text.TextUtils;

import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.config.SpKey;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:Chen
 * @Date: 2021-01-18 10:48
 * @Description: 登录缓存信息实体
 */
public class LoginCache {
    private static LoginCache loginCache;
    private ArrayList<ModelEntity> modelList;
    private String token;
    private UserEntity userEntity;
    private WarehouseEntity warehouseEntity;
    private MemberEntity memberEntity;
    private String appKey;
    private List<ModelEntity> parentModelList;//多个系统版本，存储一级菜单List

    private LoginCache() {
    }

    public static LoginCache getInstance() {
        if (loginCache == null) {
            synchronized (LoginCache.class) {
                if (loginCache == null) {
                    loginCache = new LoginCache();
                }
            }
        }
        return loginCache;
    }

    public String getToken() {
        return TextUtils.isEmpty(token) ? "" : "Bearer " + token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public void setUserInfo(UserEntity loginUserEntity) {
        this.userEntity = loginUserEntity;
        FineExApplication.component().kv().encode(SpKey.USER, loginUserEntity);
    }

    public ArrayList<ModelEntity> getModelList() {
        return modelList;
    }

    public void setModelList(ArrayList<ModelEntity> modelList) {
        this.modelList = modelList;
    }

    public List<ModelEntity> getParentModelList() {
        return parentModelList;
    }

    public void setParentModelList(List<ModelEntity> parentModelList) {
        this.parentModelList = parentModelList;
    }

    public UserEntity getUserInfo() {
        if (userEntity == null)
            return FineExApplication.component().kv().decodeParcelable(SpKey.USER, UserEntity.class);
        else return userEntity;
    }

    public void setWarehouseInfo(WarehouseEntity entity) {
        this.warehouseEntity = entity;
        FineExApplication.component().kv().encode(SpKey.WAREHOUSE, entity);
    }

    public WarehouseEntity getWarehouseInfo() {
        if (warehouseEntity == null)
            return FineExApplication.component().kv().decodeParcelable(SpKey.WAREHOUSE, WarehouseEntity.class);
        else return warehouseEntity;
    }

    public void setMemberInfo(MemberEntity entity) {
        this.memberEntity = entity;
        FineExApplication.component().kv().encode(SpKey.MEMBER, entity);
    }

    public MemberEntity getMemberInfo() {
        if (memberEntity == null)
            return FineExApplication.component().kv().decodeParcelable(SpKey.MEMBER, MemberEntity.class);
        else return memberEntity;
    }

    public String getMemberID() {
        MemberEntity memberInfo = getMemberInfo();
        if (memberInfo == null)
            return "";
        else return memberInfo.getMemberID() + "";
    }

    public String getAppKey() {
        return appKey == null ? "" : appKey;
    }

    public void setAppKey(String appKey) {
        this.appKey = appKey;
    }
}
